﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace BingZi.HY.Core.MailEngine
{
    /// <summary>
    /// .net 自带邮件类发送邮件
    /// </summary>
    public class NetMailClient : ISmtpClient
    {
        #region ISmtpClient 成员

        /// <summary>
        /// SMTP发送接口的Send方法
        /// </summary>
        /// <param name="message">邮件内容</param>
        public void Send(Message message)
        {
            MailAddress From = new MailAddress(Smtp.From, Smtp.UserName);
            MailAddress to = new MailAddress(message.Mail);

            MailMessage mailMessage = new MailMessage(From, to);
            mailMessage.Subject = message.Subject;//设置邮件主题 
            mailMessage.IsBodyHtml = true;//设置邮件正文为html格式 
            mailMessage.Body = message.Body;//设置邮件内容 
            mailMessage.BodyEncoding = Encoding.UTF8;//设置字符集

            try
            {
                SmtpClient client = new SmtpClient(Smtp.Host);
                //设置发送邮件身份验证方式 
                System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(Smtp.Account, Smtp.Password);
                client.Credentials = basicAuthenticationInfo;
                client.Port = Smtp.Port;
                client.EnableSsl = Smtp.SSL;
                client.Send(mailMessage);

            }
            catch (Exception ex)
            {


            }

        }

        #endregion
    }
}
